package com.cgsd.heyu.ui.login;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.library.adapter.ClickItemListener;
import com.android.library.utils.BeanUtils;
import com.android.library.utils.StringUtils;
import com.android.library.utils.SystemUtil;
import com.android.library.utils.ToastHelper;
import com.android.library.wedgets.TitleBarView;
import com.cgsd.heyu.R;
import com.cgsd.heyu.adapter.AvatarSelectAdapter;
import com.cgsd.heyu.common.AppMobclickAgent;
import com.cgsd.heyu.common.Constants;
import com.cgsd.heyu.common.UserModeConfig;
import com.cgsd.heyu.entity.DefaultAvatarEntity;
import com.cgsd.heyu.entity.EventMessage;
import com.cgsd.heyu.presenter.user.UserContract;
import com.cgsd.heyu.presenter.user.UserPresenter;
import com.cgsd.heyu.ui.BaseActivity;
import com.cgsd.heyu.ui.common.TabActivity;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.bean.SHARE_MEDIA;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxing
 * Description 图像选择页面
 * CreateData: 2020/11/2
 */
public class AvatarSelectActivity extends BaseActivity<UserContract.Presenter> implements ClickItemListener, TitleBarView.TitleBarClickListener, View.OnClickListener,UserContract.UpdateInfoView {
    /**
     * 创建图像
     */
    public static final int CREATE_AVATAR = 1;
    /**
     * 更换头像
     */
    public static final int UPDATE_AVATAR = 2;
    private AvatarSelectAdapter adapter;
    private List<DefaultAvatarEntity> data = new ArrayList<>();
    private TextView tvConfirm,tvWeixin;
    private int type;
    private UserModeConfig updateImage;

    public static void  actionStart(Activity activity,int type){
        Intent intent = new Intent(activity,AvatarSelectActivity.class);
        intent.putExtra("type",type);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_avatar_select;
    }

    @Override
    protected void initView() {
        new UserPresenter(this);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvWeixin = findViewById(R.id.tv_weixin);

        RecyclerView mRecyclerView = findViewById(R.id.mRecyclerView);
        adapter = new AvatarSelectAdapter(this,data);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
        mRecyclerView.setAdapter(adapter);

        adapter.setClickItemListener(this);
        mTitleBar.setTitleBarClickListener(this);
        tvConfirm.setOnClickListener(this);
        tvWeixin.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        updateImage = new UserModeConfig();
        type = getIntent().getIntExtra("type",CREATE_AVATAR);
        if (type == UPDATE_AVATAR){
            AppMobclickAgent.onEvent(AppMobclickAgent.GENGHUANTOUXIANGYE);
//            tvWeixin.setVisibility(View.VISIBLE);
            tvConfirm.setText(getString(R.string.save_select_avatar));
            mTitleBar.getTvRight().setVisibility(View.GONE);
            mTitleBar.getIvLeft().setVisibility(View.VISIBLE);
        }else {
            AppMobclickAgent.onEvent(AppMobclickAgent.XUANZETOUXIANGYE);
//            tvWeixin.setVisibility(View.GONE);
            tvConfirm.setText(getString(R.string.confirm_select_avatar));
            mTitleBar.getTvRight().setVisibility(View.VISIBLE);
            mTitleBar.getIvLeft().setVisibility(View.GONE);
        }
        mPresenter.getDefaultAvatar();
    }

    @Override
    public void clickItem(View view, int position) {
        if (adapter.getSelectPosition() != position){
            adapter.setSelectPosition(position);
            updateImage.headImgUrl = data.get(position).defaultHeadUrl;
        }
    }

    @Override
    public void leftClick() {
        finish();
    }

    @Override
    public void rightClick() {
        AppMobclickAgent.onEvent(AppMobclickAgent.TOUXIANG_XUNI);
        if (BeanUtils.isEmpty(data)){
            updateAvatarSuccess();
        }else {
            updateImage.headImgUrl = data.get(0).defaultHeadUrl;
            mPresenter.updateInfo(updateImage);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_confirm:
                AppMobclickAgent.onEvent(AppMobclickAgent.TOUXIANG_XUNI);
                if (StringUtils.isEmpty(updateImage.headImgUrl)){
                    updateAvatarSuccess();
                }else {
                    mPresenter.updateInfo(updateImage);
                }
                break;
            case R.id.tv_weixin:
                AppMobclickAgent.onEvent(AppMobclickAgent.TOUXIANG_WEIXIN);
                if (!SystemUtil.isWeixinAvilible(this)){
                    ToastHelper.showDefaultToast("未安装微信");
                    return;
                }
                getWeixinAvatar();
                break;
            default:
        }
    }

    @Override
    public void updateAvatarSuccess() {
        dismissCommonSubmitDialog();
        UserModeConfig.getInstance().headImgUrl = updateImage.headImgUrl;
        UserModeConfig userModeConfig = UserModeConfig.getInstance();
        userModeConfig.headImgUrl = updateImage.headImgUrl;
        UserModeConfig.setUserModeConfig(userModeConfig);
        if (CREATE_AVATAR == type){
            startActivity(TabActivity.getIntent(this));
            finish();
        }else {
            EventBus.getDefault().post(new EventMessage<>(Constants.EventCode.USER_REFRESH));
            finish();
        }
    }

    private void getWeixinAvatar(){
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);
        UMShareAPI.get(this).setShareConfig(config);
        UMShareAPI.get(AvatarSelectActivity.this).getPlatformInfo(AvatarSelectActivity.this, SHARE_MEDIA.WEIXIN, authListener);
    }

    private UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            updateImage.headImgUrl = data.get("iconurl");
            mPresenter.updateInfo(updateImage);
        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            showToast("授权失败");
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
        }
    };

    @Override
    public boolean isRestricted() {
        return true;
    }

    @Override
    public void updateNickNameSuccess() {

    }

    @Override
    public void updateSexSuccess() {

    }

    @Override
    public void userAvatarList(List<DefaultAvatarEntity> entities) {
        if (BeanUtils.isEmpty(entities)){
            return;
        }
        data.clear();
        data.addAll(entities);
        adapter.notifyDataSetChanged();
        updateImage.headImgUrl = data.get(adapter.getSelectPosition()).defaultHeadUrl;
    }
}
